package com.sino.androiddesign.customView


import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R



/**
 * A simple [Fragment] subclass.
 *
 */
class RegionFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_region, container, false)
    }


}



/**
 * 区域
 * 参考:https://blog.csdn.net/harvic880925/article/details/39056701
 */

/**
 *   region 创建一个空的区域或者固定大小的区域
 */

class RegionView : View {

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)

    init {

        mPaint.color = Color.RED
        mPaint.strokeWidth = 5f
        mPaint.style = Paint.Style.STROKE
    }

    override fun onDraw(canvas: Canvas?) {
//        val rgn = Region(10, 10, 100, 100)
//        //set 会重新赋值一个新的区域给原来的区域 并改变
//        rgn.set(100,100,200,200)
//        drawRegion(canvas!!, rgn, mPaint)
//
        // 使用setPath 构造不规则区域
        mPaint.color = Color.GREEN
        val  ovalPath = Path()
        ovalPath.addOval(RectF(50f,50f,200f,500f), Path.Direction.CCW)
        //传入一个比椭圆区域更小的矩形区域,取其交集
        val rgn2 = Region()
        rgn2.setPath(ovalPath, Region(50, 50, 200, 200))
        drawRegion(canvas!!, rgn2, mPaint)


    }

    private fun drawRegion(canvas: Canvas, rgn: Region, mPaint: Paint) {
        val iter = RegionIterator(rgn)
        val rect = Rect()

        while (iter.next(rect)) {
            canvas.drawRect(rect, mPaint)
        }
    }
}
